/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.entity;

import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.google.common.collect.Lists;



@Entity
@Table(name="os_wfentry")
public class HibernateWorkflowEntry extends AbstractWorkflowEntry {

		
	/**
	 * 
	 */
	private static final long serialVersionUID = -4321668198922613641L;
	private List<HibernateCurrentStep> currentSteps = Lists.newArrayList();
	private List<HibernateHistoryStep> historySteps = Lists.newArrayList();
	
	private HTMLDocument htmlDocument;
	private OfficeDocument officeDocument;
	private ArchiveDocument archiveDocument;
	private Set<Attachment> attachment;
		
	
	
	
	// ~ Methods
	// ////////////////////////////////////////////////////////////////

	public HibernateWorkflowEntry() {}


	public HibernateWorkflowEntry(long id) {
		
		this.id =  id;
	}
		
	
	@Transient
	@Override
	public boolean isInitialized() {
		return state > 0;
	}

	public void addCurrentSteps(HibernateCurrentStep step) {
	
		step.setEntry(this);
		getCurrentSteps().add(step);
	}

	public void addHistorySteps(HibernateHistoryStep step) {
		step.setEntry(this);
		getHistorySteps().add(step);
	}

	public void removeCurrentSteps(HibernateCurrentStep currentStep) {
		getCurrentSteps().remove(currentStep);
	}


	@ManyToOne
	@Cascade({CascadeType.ALL,CascadeType.DELETE,CascadeType.SAVE_UPDATE})
	@NotFound(action=NotFoundAction.IGNORE)
	@JoinColumn(name="office_id",nullable=true,unique=true)	
	@JSON(serialize=false)
	public OfficeDocument getOfficeDocument() {
		return officeDocument;
	}

	public void setOfficeDocument(
			OfficeDocument officeDocument) {
		this.officeDocument = officeDocument;
    
	}

	@ManyToOne
	@Cascade({CascadeType.ALL,CascadeType.DELETE,CascadeType.SAVE_UPDATE})
	@NotFound(action=NotFoundAction.IGNORE)
	@JoinColumn(name="html_id",nullable=true,unique=true)	
	@JSON(serialize=false)
	public HTMLDocument getHtmlDocument() {
		return htmlDocument;
	}

	public void setHtmlDocument(HTMLDocument htmlDocument) {
		this.htmlDocument = htmlDocument;
	}

	@OneToMany(orphanRemoval=true)	
	@Cascade({CascadeType.ALL,CascadeType.DELETE})
	@JSON(serialize=false)
	public Set<Attachment> getAttachment() {
		return attachment;
	}

	public void setAttachment(Set<Attachment> attachment) {
		this.attachment = attachment;
	}


	@OneToMany(orphanRemoval=true,mappedBy="entry")		
	@Cascade({CascadeType.ALL,CascadeType.DELETE})
	@JSON(serialize=false)
	public List<HibernateCurrentStep> getCurrentSteps() {
		return currentSteps;
	}


	public void setCurrentSteps(List<HibernateCurrentStep> currentSteps) {
		this.currentSteps = currentSteps;
	}


	@OneToMany(mappedBy="entry")	
	@Cascade({CascadeType.ALL})
	@JSON(serialize=false)
	public List<HibernateHistoryStep> getHistorySteps() {
		return historySteps;
	}


	public void setHistorySteps(List<HibernateHistoryStep> historySteps) {
		this.historySteps = historySteps;
	}


	public void setArchiveDocument(ArchiveDocument archiveDocument) {
		this.archiveDocument = archiveDocument;
	}

	@ManyToOne
	@Cascade({CascadeType.SAVE_UPDATE})
	@NotFound(action=NotFoundAction.IGNORE)
	@JoinColumn(name="archived_id",nullable=true,unique=true)	
	@JSON(serialize=false)
	public ArchiveDocument getArchiveDocument() {
		return archiveDocument;
	}


}
